package com.thinkgem.jeesite.modules.ta.servlet;

import java.io.*;
import java.net.URLEncoder;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ShowWord
 */
public class ShowWord extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ShowWord() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Connection conn = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager
					.getConnection(
							"jdbc:oracle:thin:@192.168.253.201:1521:orcl",
							"jsdb", "ok");
			Statement stmt = conn.createStatement();
			String id = request.getParameter("aid").toString();
			String sql = "select data from st_attachment where aid = '" + id
					+ "'";
			InputStream in = null;
			ResultSet rs = stmt.executeQuery(sql);
			if (rs.next()) {
				in = rs.getBinaryStream("data");
			}
			OutputStream toClient = response.getOutputStream();
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			response.reset();
			response.setContentType("application/msword;charset=GB2312");
			int len = 10 * 1024 * 1024;
			byte[] P_Buf = new byte[len];
			int i;
			while ((i = in.read(P_Buf)) != -1) {
				toClient.write(P_Buf, 0, i);
			}
			in.close();
			toClient.flush();
			toClient.close();
			try {
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			System.out.println("出现异常: " + e.getMessage());
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
